// src/router/index.js
import { createBrowserRouter, redirect } from "react-router-dom";
import Login from "@/pages/Login";
import Home from "@/pages/Home";
import HomeContent from "@/pages/Home/HomeContent";
import UserList from '@/pages/Home/UserManagement/UserList';
import DepartmentTree from '@/pages/Home/DepartmentManagement/DepartmentTree';
import RoleList from '@/pages/Home/RoleManagement/RoleList';
import DataSourceManagement from '@/pages/Home/DataSourceManagement';
import ContentDistribution from '@/pages/Home/ContentDistribution';
import SystemOperations from '@/pages/Home/SystemOperations';
import MetadataDrivenDataManagement from '@/pages/Home/MetadataDriven/DataManagement';
import DataAnalysis from '@/pages/Home/DataAnalysis';
import PermissionOverview from '@/pages/Home/PermissionOverview';
import { useAppSelector } from "@/store/hook";

// 创建一个包装组件来处理认证逻辑
const ProtectedRoute = ({ children }) => {
  const token = useAppSelector(state => state.user.token);
  if (!token) {
    // 如果没有token，重定向到登录页
    throw redirect("/login");
  }
  return children;
};

const router = createBrowserRouter([
  {
    path: '/login',
    element: <Login />
  },
  {
    path: '/',
    element: <ProtectedRoute><Home /></ProtectedRoute>,
    children: [
      {
        index: true,
        element: <HomeContent />,
      },
      {
        path: 'user-management',
        element: <UserList />,
      },
      {
        path: 'department-management',
        element: <DepartmentTree />,
      },
      {
        path: 'role-management',
        element: <RoleList />,
      },
      {
        path: 'data-source',
        element: <DataSourceManagement />,
      },
      {
        path: 'content-distribution',
        element: <ContentDistribution />,
      },
      {
        path: 'system-operations',
        element: <SystemOperations />,
      },
      {
        path: 'data-analysis',
        element: <DataAnalysis />,
      },
      {
        path: 'metadata-driven',
        element: <MetadataDrivenDataManagement />,
      },
      {
        path: 'permission-overview',
        element: <PermissionOverview />,
      }
    ]
  }
]);

export default router;
